package com.aisile.crowd_funding.system.service.impl;

import com.aisile.crowd_funding.common.vo.Tree;
import com.aisile.crowd_funding.system.mapper.SysMenuMapper;
import com.aisile.crowd_funding.system.mapper.SysRoleMenuMapper;
import com.aisile.crowd_funding.system.pojo.SysMenu;
import com.aisile.crowd_funding.system.pojo.SysMenuExample;
import com.aisile.crowd_funding.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ImMenuService implements MenuService {
    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return sysMenuMapper.deleteByPrimaryKey(id);
    }




    public int selectCount(Long menuId) {
        SysMenuExample example=new SysMenuExample();
        SysMenuExample.Criteria criteria = example.createCriteria();
        //把要删除的id 当做父Id来查询是否有子目录
        criteria.andParentIdEqualTo(menuId);
        List<SysMenu> sysMenus=sysMenuMapper.selectByExample(example);
        return sysMenus.size();
    }

    @Override
    public int insert(SysMenu record) {
        record.setGmtCreate(new Date());
        record.setGmtCreate(new Date());
        return sysMenuMapper.insert(record);
    }

    @Override
    public SysMenu selectByPrimaryKey(Long id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(SysMenu sysMenu) {
        return sysMenuMapper.updateByPrimaryKey(sysMenu);
    }

    @Override
    public List<SysMenu> selectByExample(SysMenuExample sysMenuExample) {
        return sysMenuMapper.selectByExample(sysMenuExample);
    }

    /**
     * 生成树
     */

    @Override
    public Tree getTree() {
        //查询出所有菜单数据
        List<SysMenu> sysMenus=sysMenuMapper.selectByExample(null);
        //把查询出来的菜单数据转化成tree
        List<Tree> treeList=new ArrayList<>();
        for(SysMenu menu:sysMenus){
            //创建tree对象
            Tree tree=new Tree();
            //把菜单的ID赋值给tree的ID
            tree.setId(menu.getMenuId()==null?"0":menu.getMenuId().toString());
            //把菜单名赋值给tree的text
            tree.setText(menu.getName());
            //把菜单的父ID赋值给tree的parentID
            tree.setParentId(menu.getParentId().toString());
            //把遍历得到的每一个tree对象装到tree集合
            treeList.add(tree);
        }
        return ImMenuService.buildTree(treeList);
    }

    @Override
    public Tree getEditTree(Long roleId) {
        List<SysMenu> sysMenus=sysMenuMapper.selectByExample(null);
        //把查询出来的菜单数据转化成tree
        List<Tree> treeList=new ArrayList<>();
        List<Long> mid=sysRoleMenuMapper.selectMenuId(roleId);
        for(SysMenu menu:sysMenus){
            //创建tree对象
            Tree tree=new Tree();
            //把菜单的ID赋值给tree的ID
            tree.setId(menu.getMenuId()==null?"0":menu.getMenuId().toString());
            //把菜单名赋值给tree的text
            tree.setText(menu.getName());
            //把菜单的父ID赋值给tree的parentID

            tree.setParentId(menu.getParentId().toString());
            //判断选中未选中
            Map<String,Object> state=new HashMap<>();

            if(mid.contains(menu.getMenuId())){
                     state.put("selected",true);
            }else {
                     state.put("selected",false);
            }
             tree.setState(state);
            //把遍历得到的每一个tree对象装到tree集合
            treeList.add(tree);
        }
        return ImMenuService.buildTree(treeList);
    }

    //写一个本类的静态方法返回一个tree
    public static Tree buildTree(List<Tree> treeList){
        //定义第一层节点
        List<Tree> topTreeList=new ArrayList<>();
        for(Tree chid:treeList){
            //判断是否是第一层节点就把它放入集合 进行一次筛选
            //如果它的父节点是空或者父节点是0那么就认为它是第一层节点筛选所有的
            if(chid.getParentId()==null||"0".equals(chid.getParentId())){
                topTreeList.add(chid);
                continue;
            }
            //如果当前节点不是第一层几点 那么就找到该节点的父节点
            for(Tree parent:treeList){
                //找到父节点如果chid的父ID等于parent的ID 因为是两层循环进行比较
                //chid的父id是 内循环的ID 那么内循环的这个parent 就是它的父节点
                 if(chid.getParentId().equals(parent.getId())){
                     //如果找到父节点 children 属性是一个集合
                     //把子节点放入children集合确定他们的父子关系
                     parent.getChildren().add(chid);
                     chid.setHasParent(true);
                     parent.setChildren(true);
                     continue;
                 }
            }
        }
        //因为要返回的是一个tree对象所以在这里就再给它创建一个根节点
        Tree rootTree=new Tree();
        rootTree.setId("-1");
        rootTree.setText("根节点");
        //把所有的节点都当成他的子节点
        rootTree.setChildren(topTreeList);
        //创建属性集合
        Map<String,Object> state=new HashMap<>();
        state.put("opened",true);
        rootTree.setState(state);
        return rootTree;
    }
}
